<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author:kane < chengjin005@163.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use app\portal\model\PortalSensitiveModel;
use cmf\controller\AdminBaseController;
use think\Db;
use app\admin\model\ThemeModel;

/**
 * Class AdminTagController 标签管理控制器
 * @package app\portal\AdminTagController
 */
class AdminSensitiveController extends AdminBaseController
{
    /**
     * 敏感词管理
     * @adminMenu(
     *     'name'   => '敏感词',
     *     'parent' => 'portal/AdminIndex/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '敏感词',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $portalSensitiveModel = new portalSensitiveModel();
        // $tags           = $portalSensitiveModel->paginate();
        
        $param = $this->request->param();
        $where = ' 1 ';
        
        if(isset($param['keyword'])){
            $where .= " and s.name like '%".$param['keyword']."%'";
            
        }
      
   
        $field  = 's.*,u.user_login';

        $join = [
            ['__USER__ u', 's.admin_id = u.id']
        ];


        // $sensitive = $portalSensitiveModel ->alias('s') ->field($field)->join($join)->where($where)->paginate(10);
        $sensitive = $portalSensitiveModel 
                  -> alias('s') 
                  -> field($field)
                  -> join($join)
                  -> where($where)
                  -> paginate(10, false, ['query'=>request()->param()]);

        $this->assign("arrStatus", $portalSensitiveModel::$STATUS);

        $this->assign('keyword', isset($param['keyword']) ? $param['keyword'] : '');

        $this->assign("sensitive", $sensitive);
        $this->assign('page', $sensitive->render());

        $actions = ['add', 'edit', 'delete'];
        $this->assign('actions', actions_check($this->request->module().'/'.$this->request->controller().'/', $actions));

        return $this->fetch();
    }

    /**
     * 添加敏感词
     * @adminMenu(
     *     'name'   => '添加敏感词',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加敏感词',
     *     'param'  => ''
     * )
     */
    public function add()
    {   
        // $portalSensitiveModel = new portalSensitiveModel();
        // $this->assign("arrStatus", $portalSensitiveModel::$STATUS);
        return $this->fetch();
    }


    /**
     * 编辑敏感词
     * @adminMenu(
     *     'name'   => '编辑敏感词',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑敏感词',
     *     'param'  => ''
     * )
     */
    public function edit()
    {
        $id = $this->request->param('id', 0, 'intval');

        $portalSensitiveModel = new portalSensitiveModel();
        $post                 = $portalSensitiveModel->where('id', $id)->find();

        // $this->assign("arrStatus", $portalSensitiveModel::$STATUS);
        $this->assign('post', $post);

        return $this->fetch();
    }

    /**
     * 添加敏感词提交
     * @adminMenu(
     *     'name'   => '添加敏感词提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加敏感词提交',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {

        $arrData = $this->request->param();
    
 
        $result = $this->validate($arrData, 'AdminSensitive');
        if ($result !== true) {
            $this->error($result);
        }

        $arrData['add_time'] = time();
        $arrData['admin_id'] = cmf_get_current_admin_id();


        $portalSensitiveModel = new portalSensitiveModel();
        $portalSensitiveModel->isUpdate(false)->allowField(true)->save($arrData);

        //操作日志
        admin_log('', 'add', 'sensitive_words');

        $this->success(lang("SAVE_SUCCESS"), url('AdminSensitive/index'));

    }


    /**
     * 编辑敏感词提交
     * @adminMenu(
     *     'name'   => '编辑敏感词提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑敏感词提交',
     *     'param'  => ''
     * )
     */
    public function editPost()
    {

        if ($this->request->isPost()) {
            $data   = $this->request->param();
            
            $result = $this->validate($data, 'AdminSensitive');
            if ($result !== true) {
                $this->error($result);
            }

            $data['update_time'] = time();

            $portalSensitiveModel = new portalSensitiveModel();
          
            
            
            $portalSensitiveModel->isUpdate(true)->allowField(true)->data($data,true)->save();

            //操作日志
            admin_log('', 'edit', 'sensitive_words');
            
            $this->success('保存成功!', url('AdminSensitive/index'));

        }
    }


    /**
     * 更新敏感词状态
     * @adminMenu(
     *     'name'   => '更新标签状态',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '更新标签状态',
     *     'param'  => ''
     * )
     */
    public function upStatus()
    {
        $intId     = $this->request->param("id");
        $intStatus = $this->request->param("status");
        $intStatus = $intStatus ? 1 : 0;
        if (empty($intId)) {
            $this->error(lang("NO_ID"));
        }

        $portalSensitiveModel = new portalSensitiveModel();
        $portalSensitiveModel->isUpdate(true)->save(["status" => $intStatus], ["id" => $intId]);

        $this->success(lang("SAVE_SUCCESS"));

    }

    /**
     * 删除敏感词
     * @adminMenu(
     *     'name'   => '删除敏感词',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除敏感词',
     *     'param'  => ''
     * )
     */
    public function delete()
    {
        $intId = $this->request->param("id", 0, 'intval');

        if (empty($intId)) {
            $this->error(lang("NO_ID"));
        }
        $portalSensitiveModel = new portalSensitiveModel();

        $portalSensitiveModel->where(['id' => $intId])->delete();
        Db::name('portal_sensitive_post')->where('sensitive_id', $intId)->delete();

        //操作日志
        admin_log('', 'delete', 'sensitive_words');

        $this->success(lang("DELETE_SUCCESS"));
    }

    // 敏感词检测
    public function replace(){
        $param  = $this->request->param();
        $data   = $param['data'];
        $field  = $param['field'];
        $result = ['error'=>0];
        
        if( empty($data) ){
            return $result;
        }

        $html  = '';
        foreach ($data as $k => $v) {
            // 遍历文章下的数据，逐个进行敏感词检查
            if( !empty($v) ){
                $checkRes = check_sensitive_words($field[$k], $v);    //敏感词检查
                if( !empty($checkRes) ){
                    $html .= $checkRes;
                }
            }
        }

        if( !empty($html) ){
            $html            = '<div class="sensitive-word-confirm">'.$html.'<p class="is-confirm">是否确认替换？</p></div>';
            $result['error'] = 1;
            $result['html']  = $html;
        }

        return $result;
    }
}
